// pages/course/course.js
import{getCourseListRequest} from '../../api/main';
 const courseCacheKey="course";
 const courseColorCacheKey = "courseColor"
 const courseNowWeekCacheKey = "courseNowWeek"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowMonth:1,
    startDate: '2023/08/28',
    nowWeek: 1, //当前周
    totalWeek: 20, //周总数
    showSwitchWeek: false,
    weekDayCount: 7,
    weekIndexText: ['一', '二', '三', '四', '五', '六', '日'],
    courseList: [],
    colorList:[
      '#0C356A',
      '#3A4D39',
      '#FF6C22',
      '#072541',
      '#FF4B91',
      '#D83F31',
      '#BB2525',
      '#3D246C',
      '#EF6262'
    ],
   
    //首次不滑动
    firstEntry:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      windowWidth
    } = wx.getSystemInfoSync();
    this.setData({
      windowWidth
    })
    this.getNowWeek();
    this.getData();
    this.getTodayDate();
  },

  switchWeek(e) {
    const week = e.currentTarget.dataset.week;
    this.setData({
      showSwitchWeek: false
    })
 this.switchWeekFn(week)
  },

  selectWeek() {
    this.setData({
      showSwitchWeek: true
    })
  },
  switchWeekFn(week){
    this.setData({
      nowWeek: week,
    })
    this.getWeekDates()
  },
  hideSwitchWeek() {
    this.setData({
      showSwitchWeek: false
    })
  },

  getWeekDates() {
    const startDate = new Date(this.data.startDate)

    const addTime = (this.data.nowWeek - 1) * 7 * 24 * 60 * 60 * 1000

    const firstDate = startDate.getTime() + addTime;
    const {month:nowMonth} = this.getDateObject(new Date(firstDate))
    const weekCalendars = [];

    for (let i = 0; i < this.data.weekDayCount; i++) {
      const date= new Date(firstDate + i * 24 * 60 * 60 * 1000)

      const {day} = this.getDateObject(date);
      weekCalendars.push(day);
    }
    this.setData({
      nowMonth,
      weekCalendars
    })
  },

  getDateObject(date = new Date()){
    const year = date.getFullYear ();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    return{
      year,
      month,
      day
    }
  },
getNowWeek(){
const nowDate = new Date().getTime();
const startDate = new Date(this.data.startDate);
 const time = nowDate-startDate
  let  nowWeek  =Math.ceil(time /1000/60/60/24/7)
  this.setData({
    nowWeek
  })
  this.getWeekDates()
  wx.setStorageSync(courseNowWeekCacheKey,nowWeek)
},

getData(){
  const cache = wx.getStorageSync(courseCacheKey);
  const cacheCourseColor = wx.getStorageSync(courseColorCacheKey);
  
  if (cache) {
    this.setData({
      courseList:cache,
    })
    
  }else{
    this.update()
  }
  if (cacheCourseColor) {
    this.setData({
      courseColor:cacheCourseColor,
    })
  } else{
    this.buildCourseColor()
  }

},

update(){
  getCourseListRequest().then((res) =>{
  
    this.setData({
      courseList:res.data,
    })
    this.buildCourseColor();
    wx.setStorageSync(courseCacheKey,res.data)
 
   })
},
swiperSwitchWeek(e){
  if (e.detail.source =='') {
    this.setData({
      firstEntry:false
    })
    return
  }
  const index = e.detail.current;
  this.switchWeekFn(index+1)
},
navCourseDetail(e){
    const index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: `/pages/course-detail/course-detail?info=${
        JSON.stringify(this.data.courseList[index])
      }`,
    })
},
buildCourseColor(){
  const courseColor={};
    let colorIndex= 0;
  this.data.courseList.forEach(item => {
   
    if (courseColor[item.name] == undefined) {
      courseColor[item.name]=this.data.colorList[colorIndex];
      colorIndex++
    }
  })
  this.setData({
    courseColor
  })
  wx.setStorageSync(courseColorCacheKey,courseColor)
},
getTodayDate(){
const {month:todayMonth,
day:todayDay} = this.getDateObject();
this.setData({
  todayMonth,
  todayDay
});

},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})